//
//  DZTabBarController.m
//  Weico
//
//  Created by 大泽 on 15/7/21.
//  Copyright (c) 2015年 刘洪泽. All rights reserved.
//

#import "DZTabBarController.h"
#import "DZHomeViewController.h"
#import "DZMessageViewController.h"
#import "DZDiscoverViewController.h"
#import "DZProfileViewController.h"
#import "DZNavigationController.h"
#import "DZTabBar.h"
@interface DZTabBarController ()

@end

@implementation DZTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    DZHomeViewController *home = [[DZHomeViewController alloc] init];
    [self addChildVc:home title:@"首页" imageName:@"tabbar_home" selectImageName:@"tabbar_home_selected"];
    
    DZMessageViewController *message = [[DZMessageViewController alloc] init];
    [self addChildVc:message title:@"消息" imageName:@"tabbar_message_center" selectImageName:@"tabbar_message_center_selected"];
    
    DZDiscoverViewController *discover = [[DZDiscoverViewController alloc] init];
    [self addChildVc:discover title:@"发现" imageName:@"tabbar_discover" selectImageName:@"tabbar_discover_selected"];
    
    DZProfileViewController *profile = [[DZProfileViewController alloc] init];
    [self addChildVc:profile title:@"我" imageName:@"tabbar_profile" selectImageName:@"tabbar_profile_selected"];
    
    DZTabBar *tabBar = [[DZTabBar alloc] init];
    
    [self setValue:tabBar forKeyPath:@"tabBar"];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    DZLog(@"%@", self.tabBar.subviews);
}


- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectImageName:(NSString *)selectImageName
{
    // 设置标题文字
    childVc.title = title;
    
    // 设置tabbar的图片
    UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    childVc.tabBarItem.selectedImage = selectedImage;
    childVc.tabBarItem.image = image;
    
    // 设置tabbar的文字样式
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    [childVc.tabBarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    
    NSMutableDictionary *selectAttrs = [NSMutableDictionary dictionary];
    selectAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [childVc.tabBarItem setTitleTextAttributes:selectAttrs forState:UIControlStateSelected];
    
    // 包装导航栏
    DZNavigationController *nav = [[DZNavigationController alloc] initWithRootViewController:childVc];

    [self addChildViewController:nav];
}

@end
